import {Injectable} from '@angular/core';
import {ParkingArea} from '../types/resp/ParkingArea';
import {ParkingAreaForm} from '../types/req/ParkingAreaForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {ParkingAreaQueryForm} from '../types/req/ParkingAreaQueryForm';

@Injectable({
  providedIn: 'root'
})
export class ParkingAreaService {
  parkingAreas: ParkingArea[] = [
    {id: 1, areaNumber: '010', buildingId: 1, buildingName: '北区1栋', floorNumber: '负一', residentRoomId: 1, residentRoomNumber: '301', licensePlate: '粤A11111', status: 20, createTime: '2024-01-01 12:12:12', modifyTime: '2024-02-02 13:13:13'},
    {id: 2, areaNumber: '021', buildingId: 2, buildingName: '北区1栋', floorNumber: '负一', residentRoomId: 1, residentRoomNumber: '402', licensePlate: '粤A11112', status: 10, createTime: '2024-02-02 12:12:12', modifyTime: '2024-03-03 13:13:13'},
    {id: 3, areaNumber: '032', buildingId: 3, buildingName: '南区1栋', floorNumber: '负二', residentRoomId: 1, residentRoomNumber: '503', licensePlate: '粤A11113', status: 20, createTime: '2024-03-03 12:12:12', modifyTime: '2024-04-04 13:13:13'},
    {id: 4, areaNumber: '043', buildingId: 4, buildingName: '南区2栋', floorNumber: '负二', residentRoomId: 1, residentRoomNumber: '604', licensePlate: '粤A11114', status: 10, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'},
    {id: 5, areaNumber: '038', buildingId: 5, buildingName: '东区1栋', floorNumber: '负一', residentRoomId: 1, residentRoomNumber: '204', licensePlate: '粤A11115', status: 20, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'},
    {id: 6, areaNumber: '029', buildingId: 6, buildingName: '东区2栋', floorNumber: '负一', residentRoomId: 1, residentRoomNumber: '404', licensePlate: '粤A11116', status: 30, createTime: '2024-04-04 12:12:12', modifyTime: '2024-05-05 13:13:13'}
  ]
  constructor() {
  }

  save (parkingAreaForm: ParkingAreaForm): Observable<Result<void>> {
    console.log('parkingAreaApi save params', parkingAreaForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (parkingAreaQueryForm: ParkingAreaQueryForm): Observable<Result<Page<ParkingArea>>> {
    console.log('parkingAreaApi find params', parkingAreaQueryForm)
    return MockApi.queryPageSuccessfully(this.parkingAreas).pipe(
      map((response: Response<Result<Page<ParkingArea>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<ParkingArea>> {
    console.log('parkingAreaApi findById id', id)
    let target = {}
    for (const a of this.parkingAreas) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<ParkingArea>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('parkingAreaApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
